package controller.uu;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.dao.PhotoDaoLocal;
import model.dao.TagDaoLocal;
import model.dao.UserDaoLocal;
import model.entity.Album;
import model.entity.Photo;
import model.entity.Tag;
import model.entity.User;
import controller.CUtil;

/**
 * Servlet implementation class SSearchResults
 */
public class SSearchResults extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private PhotoDaoLocal photoDao;
	
	@EJB
	private UserDaoLocal userDao;
	
	@EJB
	private TagDaoLocal tagDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SSearchResults() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String searchString=(String)request.getParameter("searchString");
		boolean titleAndDescription;
		boolean author;
		boolean dimensionsg;
		boolean dimensionsl;
		boolean rateg;
		boolean ratel;
		boolean tag;
		
		String[] results=request.getParameterValues("tandd");
		boolean result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		titleAndDescription=result;
		
		results=request.getParameterValues("author");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		author=result;
		
		results=request.getParameterValues("dimensionsg");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		dimensionsg=result;
		
		results=request.getParameterValues("dimensionsl");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		dimensionsl=result;
		
		results=request.getParameterValues("rateg");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		rateg=result;
		
		results=request.getParameterValues("ratel");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		ratel=result;
		
		results=request.getParameterValues("tag");
		result=false;
		if(results!=null && results.length!=0){
			result=true;
		}
		tag=result;
		
		Set<Photo> resultList=new HashSet<Photo>();
		List<Photo> crResult;
		if(titleAndDescription){
			crResult=photoDao.searchTitleOrDescription(searchString);
			if(crResult!=null){
				resultList.addAll(crResult);
			}
		}
		
		if(author){
			List<User> users=userDao.searchByName(searchString);
			if(users!=null){
				for(User u:users){
					for(Album a:u.getAlbums()){
						if(!a.getFavorite()){
							for(Photo p:a.getPhotos()){
								resultList.add(p);
							}
						}
					}
				}
				
			}
		}
		
		boolean isNumber=false;
		double number=0;
		try{
			number=Double.parseDouble(searchString);
			isNumber=true;
		}catch(Exception e){}
		
		if(isNumber){
			if(dimensionsg){
				crResult=photoDao.searchDimensions((int)number, true);
				if(crResult!=null){
					resultList.addAll(crResult);
				}
			}
			if(dimensionsl){
				crResult=photoDao.searchDimensions((int)number, false);
				if(crResult!=null){
					resultList.addAll(crResult);
				}
			}
			if(rateg){
				crResult=photoDao.searchRate((int)number, true);
				if(crResult!=null){
					resultList.addAll(crResult);
				}
			}
			if(ratel){
				crResult=photoDao.searchRate((int)number, false);
				if(crResult!=null){
					resultList.addAll(crResult);
				}
			}
		}
		if(tag){
			List<Tag> tags=tagDao.findByContentA(searchString);
			if(tags!=null && tags.size()!=0){
				for(Tag t:tags){
					for(Photo p:t.getPhotos()){
						resultList.add(p);
					}
				}
			}
		}
		
		request.setAttribute("searchString", searchString);
		request.setAttribute("resultList", resultList);
		CUtil.redirecter(request, response, "uu/VSearchResults.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
